<?
require_once("cms/core/action/AbstractListAction.php");
require_once("cms/project/om/ProjectI18nPeer.php");

class ListAction extends AbstractListAction {

    private $log;public $dre;

    public function __construct() {
        $this->log =& LoggerManager::getLogger("ListAction");
        $this->dre = "dre";
    }

    public function activate(ActionConfig &$mapping, &$form, Request &$request, Response &$response) {

        $this->log->debug("Activating projects");
        $c = new Criteria();
        $c->add(ProjectI18nPeer::PROJECT_I18N_ID, $form->getItems(), Criteria::IN);
        $c->add(ProjectI18nPeer::ACTIVE, true);
        try {
            ProjectI18nPeer::doUpdate($c);
            $this->messages->add(ActionMessage::GLOBAL_MESSAGE,
                new ActionMessage("list.success.activate", true));
        } catch(PropelException $e) {
            $this->errors->add(ActionMessage::GLOBAL_MESSAGE,
                new ActionMessage("list.error.activate", true));
            $this->log->error($e->getMessage());
        }

    }

    public function deactivate(ActionConfig &$mapping, &$form, Request &$request, Response &$response) {

        $this->log->debug("Deactivating projects");
        $c = new Criteria();
        $c->add(ProjectI18nPeer::PROJECT_I18N_ID, $form->getItems(), Criteria::IN);
        $c->add(ProjectI18nPeer::ACTIVE, false);
        try {
            ProjectI18nPeer::doUpdate($c);
            $this->messages->add(ActionMessage::GLOBAL_MESSAGE,
                new ActionMessage("list.success.deactivate", true));
        } catch(PropelException $e) {
            $this->errors->add(ActionMessage::GLOBAL_MESSAGE,
                new ActionMessage("list.error.deactivate", true));
            $this->log->error($e->getMessage());
        }

    }

    public function delete(ActionConfig &$mapping, &$form, Request &$request, Response &$response) {

        $this->log->debug("Deleting projects");

        $c = new Criteria();
        $c->add(ProjectI18nPeer::PROJECT_I18N_ID, $form->getItems(), Criteria::IN);

        $conn = Propel::getConnection();

        try {

            $conn->begin();

            $projectI18ns = ProjectI18nPeer::doSelect($c);
            foreach($projectI18ns as $projectI18n) {
                // Delete the project itself
                if($projectI18n->getLanguageCode() ==
                        $request->getContext()->getInitParameter(_Core::DEFAULT_LANGUAGE)) {
                    $project = $projectI18n->getProject();
                    // Delete the Project and have the onDelete=cascade take care of the rest.
                    ProjectPeer::doDelete($projectI18n->getProjectId(), $conn);
                } else {
                    // Just this row.
                    ProjectI18nPeer::doDelete($projectI18n->getProjectI18nId(), $conn);
                }
            }

            $this->messages->add(ActionMessage::GLOBAL_MESSAGE,
                new ActionMessage("list.success.delete", true));

            $conn->commit();

        } catch(PropelException $e) {
            $conn->rollback();
            $this->errors->add(ActionMessage::GLOBAL_MESSAGE,
                new ActionMessage("list.error.delete", true));
            $this->log->error($e);
        }

    }

}
?>